﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqData;
using Cricket.LinqModel;

public partial class MyProfile : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Admin admin;
    AdminCredential adminCredentail;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {

                if (Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.SuperAdmin) || Convert.ToInt32(cookie["UserType"]) == Convert.ToInt32(Enumeration.UserType.Admin))
                {
                    
                        fillAdmin(cookie["AdminId"]);
                    
                }
                else
                {
                    Response.Redirect("Logout.aspx");
                }
            }
        }
    }

    private void fillAdmin(string adminId)
    {
        layerSuccess.InnerHtml = "";
        layerSuccess.Attributes.Add("style", "display:none;");
        admin = new Admin(_dataObject);
        List<AdminResult> adminResult = new List<AdminResult>();
        admin.Id = Convert.ToInt32(adminId);
        adminResult = admin.GetAdminDetailsById(admin.Id);
        if (adminResult.Count > 0)
        {
            txtFirstName.Text = adminResult[0].FirstName;
            txtLastName.Text = adminResult[0].LastName;
            txtPhone.Text = adminResult[0].Phone;
            txtEmailAddress.Text = adminResult[0].Email;
            txtCity.Text = adminResult[0].City;
            txtState.Text = adminResult[0].State;
            rdoCommonRate.SelectedValue = Convert.ToString(adminResult[0].CommonRate);
            hdn1.Value = Convert.ToString(adminResult[0].AdminCredentialId);
            txtBallToBallPassword.Text = adminResult[0].Password1;
            txtBallToBallPassword.Attributes.Add("value", adminResult[0].Password1);
            txtBallToBallPassword2.Text = adminResult[0].Password2;
            txtBallToBallPassword2.Attributes.Add("value", adminResult[0].Password2);

            hdn2.Value = Convert.ToString(adminResult[2].AdminCredentialId);
            txtPasswordScore.Text = adminResult[2].Password1;
            txtPasswordScore.Attributes.Add("value", adminResult[2].Password1);
            txtPasswordScore2.Text = adminResult[2].Password2;
            txtPasswordScore2.Attributes.Add("value", adminResult[2].Password2);

            hdn3.Value = Convert.ToString(adminResult[1].AdminCredentialId);
            txtPasswordSession.Text = adminResult[1].Password1;
            txtPasswordSession.Attributes.Add("value", adminResult[1].Password1);
            txtPasswordSession2.Text = adminResult[1].Password2;
            txtPasswordSession2.Attributes.Add("value", adminResult[1].Password2);

            

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        admin = new Admin(_dataObject);
        admin.FirstName = txtFirstName.Text;
        admin.LastName = txtLastName.Text;
        admin.Phone = txtPhone.Text;
        admin.Email = txtEmailAddress.Text;
        admin.City = txtCity.Text;
        admin.State = txtState.Text;
        admin.IsActive = true;
        admin.IsDeleted = false;
        admin.CommonRate = Convert.ToInt32(rdoCommonRate.SelectedValue);
        admin.UserType = Convert.ToInt32(cookie["UserType"]);
        if (cookie["AdminId"] != null)
        {
            admin.DateUpdated = System.DateTime.UtcNow;
            admin.Id = Convert.ToInt32(cookie["AdminId"]);
            admin.UpdateAdmin();

            adminCredentail = new AdminCredential(_dataObject);
            adminCredentail.AdminId = Convert.ToInt32(cookie["AdminId"]);
            adminCredentail.Id = Convert.ToInt32(hdn1.Value);
            adminCredentail.BatId = 1;
            adminCredentail.Password1 = txtBallToBallPassword.Text;
            adminCredentail.Password2 = txtBallToBallPassword2.Text;
            adminCredentail.UpdateAdminCredentialsMyProfile();

            adminCredentail = new AdminCredential(_dataObject);
            adminCredentail.AdminId = Convert.ToInt32(cookie["AdminId"]);
            adminCredentail.Id = Convert.ToInt32(hdn2.Value);
            adminCredentail.BatId = 2;
            adminCredentail.Password1 = txtPasswordSession.Text;
            adminCredentail.Password2 = txtPasswordSession2.Text;
            adminCredentail.UpdateAdminCredentialsMyProfile();

            adminCredentail = new AdminCredential(_dataObject);
            adminCredentail.AdminId = Convert.ToInt32(cookie["AdminId"]);
            adminCredentail.Id = Convert.ToInt32(hdn3.Value);
            adminCredentail.BatId = 3;
            adminCredentail.Password1 = txtPasswordScore.Text;
            adminCredentail.Password2 = txtPasswordScore2.Text;
            adminCredentail.UpdateAdminCredentialsMyProfile();
        }
        layerSuccess.InnerHtml = "<strong>Success!</strong> Profile updated successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
    }
}